<template>
  <el-card>
    <el-form :inline="true">
      <el-form-item label="一级分类">
        <el-select
          v-model="categoryStore.c1Id"
          placeholder="请选择"
          class="select"
          @change="handleChangeC1(categoryStore.c1Id)"
        >
          <el-option
            v-for="attr in categoryStore.c1Attrs"
            :key="attr.id"
            :label="attr.name"
            :value="attr.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select
          placeholder="请选择"
          class="select"
          v-model="categoryStore.c2Id"
          @change="handleChangeC2(categoryStore.c2Id)"
        >
          <el-option
            v-for="attr in categoryStore.c2Attrs"
            :key="attr.id"
            :label="attr.name"
            :value="attr.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select
          placeholder="请选择"
          class="select"
          v-model="categoryStore.c3Id"
          @change="handleChangeC3(categoryStore.c3Id)"
        >
          <el-option
            v-for="attr in categoryStore.c3Attrs"
            :key="attr.id"
            :label="attr.name"
            :value="attr.id"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import useCategoryStore from '@/store/modules/category'
import { ca } from 'element-plus/es/locale/index.mjs'

let categoryStore = useCategoryStore()

//分类全局组件挂载完毕，通知仓库发请求获取一级分类数据
onMounted(() => {
  GetC1()
})

//通知仓库获取一级分类的方法
const GetC1 = () => {
  categoryStore.getC1()
}

const handleChangeC1 = (id: number) => {
  //需要将二级和三级分类清空
  categoryStore.c2Id = ''
  categoryStore.c3Attrs = []
  categoryStore.c3Id = ''
  console.log(id)
  //通知仓库获取二级分类数据
  categoryStore.getC2()
}

const handleChangeC2 = (id: number) => {
  //需要将三级分类清空
  categoryStore.c3Id = ''
  console.log(id)
  //通知仓库获取三级分类数据
  categoryStore.getC3()
}

const handleChangeC3 = (id: number) => {
  console.log(id)
  //绑定数据表格
}
</script>

<style scoped>
.select {
  width: 200px;
}
</style>
